package com.qf.mpswaggershiro.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mpswaggershiro.product.entity.ProductComments;
import com.qf.mpswaggershiro.product.mapper.ProductCommentsMapper;
import com.qf.mpswaggershiro.product.service.IProductCommentsService;
import com.qf.mpswaggershiro.user.entity.Users;
import com.qf.mpswaggershiro.user.service.IUsersService;
import com.qf.mpswaggershiro.vo.DetailCommontscountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author cui
 * @since 2023-02-14
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements IProductCommentsService {
@Autowired
    IUsersService usersService;
@Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Map<String, Object> commints(Integer id, Integer pageNum, Integer limit) {
        Page<ProductComments> page = new Page<>(pageNum, limit);

        page(page,new QueryWrapper<ProductComments>().eq("product_id",id));

        //拿到ProductComments表中的  符合前端用户点击的id的 所有数据
        List<ProductComments> records = page.getRecords();
        //拿到总条数
        int total = (int) page.getTotal();
        //拿到总页数
        int current = (int) page.getCurrent();
         //拿到UserId转换成list集合中
        List<String> usersId = records.stream().map(ProductComments::getUserId).collect(Collectors.toList());
        //在user中查询userID

        List<Users> users = usersService.list(new QueryWrapper<Users>().in("user_id", usersId));

        for (Users user : users) {
            for (ProductComments record : records) {
                if (user.getUserId() ==Integer.parseInt(record.getUserId())) {
                    record.setUsername(user.getUsername());
                    record.setNickname(user.getNickname());
                    record.setUserImg(user.getUserImg());
                }
            }

        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageCount",current);
        map.put("count",total);
        map.put("list",records);

        return map;
    }

    /**
     * 好评率
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public DetailCommontscountVo detailCommontscount(Integer id) {
        List<ProductComments> product_id = list(new QueryWrapper<ProductComments>().eq("product_id", id));
        Integer goodTotal = 0;
        Integer midTotal=0;
        Integer badTotal=0;
        Integer total=0;
        Double percent = 0.0;
        for (ProductComments productComments : product_id) {
            switch (productComments.getCommType()){
                case 1:
                    goodTotal++;
                    break;
                case 0:
                    midTotal++;
                    break;
                case -1:
                    badTotal++;
                    break;
                default:
                    break;
            }
            total++;
        }
        percent = (double) goodTotal / (double) total;

        //保留两位小数  BigDecimal
        BigDecimal bigDecimal = new BigDecimal(percent);
        double v = bigDecimal.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        return new DetailCommontscountVo(goodTotal,total,midTotal,badTotal,v);
    }
}
